<!-- 群发消息 - 新增/编辑消息 -->
<template>
    <div class="rightCenter">
        <div class="editTop">
            <div class="editTopTitle" @click="cancelClick()">群发消息</div>
            <i class="icon font_family icon-yema-you"></i>
            <div class="editTopTitle">{{editVal}}</div>
        </div>
        <div class="editList">
            <div class="editListName">
                消息标题：
            </div>
            <input class="editListInpBox fontBlue" v-model.trim='titleVal' type="text" placeholder="标题不超过50个字符" maxlength="50">
            <span class="warn" v-show='titleShow'>请输入标题</span>
        </div>
        <div class="editList">
            <div class="editListName">
                头图：
            </div>
            <div class="editUpImg">
                <div>
                    <div class="editUpImgHand" v-show='!handImgVal'>
                        <i class="icon font_family icon-shangchuanzhaopian"></i>
                    </div>
                    <img class="editHandImg" v-show='handImgVal' :src="handImgVal" alt="">
                </div>
                <div>
                    <label for="fileUpImg">
                        上传
                    </label>
                    <input id="fileUpImg" accept="image/jpeg,image/jpg,image/png,image/bmp,image/gif" type="file" v-on:change="upImgChange($event)">
                    <p :class="{fontGary:!imgFontShow,fontRed:imgFontShow}">（图片大小660*510像素）</p>
                </div>
                <div class="warn" v-show='imgupShow'>
                    请上传头图
                </div>
            </div>
        </div>
        <div class="editList">
            <div class="editListName">
                消息内容：
            </div>
            <textarea id="textId" class="textBox" v-model.trim='briefVal' @input='textHeigth()' maxlength="200" placeholder="请输入200字以内的介绍"></textarea>
            <span class="warn" v-show='briefShow'>请输入200字以内的介绍</span>
        </div>
        <div class="editList">
            <div class="editListName">
                发送时间：
            </div>
            <div class="editListInpFont">
                <el-select v-model="typeVal" placeholder="请选择">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </div>
        </div>
        <div class="footButton">
            <div class="addBut" @click='addClick'>
                保 存
            </div>
            <div class="cancelBut" @click='cancelClick'>
                取 消
            </div>
        </div>
    </div>
</template>
<script>
import { mapActions } from "vuex";
import messageRequest from "../../assets/message";
import fileRequest from "../../assets/file";
export default {
  data() {
    return {
      editVal: "新增消息",
      titleVal: "", //标题
      titleShow: false,
      handImgVal: null, //头图
      imgupShow: false,
      imgFontShow: false, //头图限制文字
      briefVal: "", //消息内容
      briefShow: false,
      typeVal: "", //类型
      options: [
        { value: "", label: "立即发送" },
        { value: "1", label: "10分钟后" },
        { value: "2", label: "30分钟后" },
        { value: "3", label: "1小时后" }
      ],
      fileVal: "" // 上传文件
    };
  },
  filters: {},
  created: function() {
    let self = this;
    if (sessionStorage.getItem("TABLIST")) {
      let arr = JSON.parse(sessionStorage.getItem("TABLIST"));
      let routShow = false;
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].name == "群发消息") {
          routShow = true;
        }
      }
      if (!routShow) {
        this.$router.push({
          path: "/login"
        });
        sessionStorage.clear();
        self.$message("暂无权限");
        return;
      }
    } else {
      this.$router.push({
        path: "/login"
      });
      sessionStorage.clear();
      self.$message("暂无权限");
        return;
    }
    self.listFun();
  },
  watch: {},
  mounted() {},
  methods: {
    async listFun() {
      let self = this;
      let params = {
        page: self.pageNo,
        pageSize: self.pageSize
      };
      let getParams = `page=${params.page}&pageSize=${params.pageSize}`;
      let res = await messageRequest.webMessageListFun(getParams);
      if (res.statusCode == 200) {
        if (res.data) {
          if (res.data.records) {
            self.dataList = res.data.records;
          }
          self.totalVal = res.data.total * 1;
        }
      } else {
        self.$message(res.statusMessage);
        self.dataList = [];
      }
    },
    cancelClick() {
      this.$router.push({
        path: "/home/message"
      });
    },
    //text自动撑高
    textHeigth() {
      textId.style.height = 0;
      textId.style.height = textId.scrollHeight + "px";
    },
    //选择图片
    upImgChange(e) {
      let self = this;
      let files = e.target.files || e.dataTransfer.files;
      let fileData = files[0];
      let reader = new FileReader();
      reader.onload = function(e) {
        let data = e.target.result;
        let image = new Image();
        image.onload = function() {
          // if (image.width !== 600 || image.height !== 510) {
          //   self.imgFontShow = true;
          //   self.$message({
          //     type: "info",
          //     message: "上传图片尺寸不符，请重新上传"
          //   });
          //   document.getElementById("fileUpImg").value = "";
          // } else if (fileData.size > 1048576) {
          //   self.imgFontShow = true;
          //   self.$message({
          //     type: "info",
          //     message: "上传图片大小过大，请重新上传"
          //   });
          //   document.getElementById("fileUpImg").value = "";
          // } else {
          //   self.imgFontShow = false;
          //   self.uploadBanner(files[0]);
          // }
          self.uploadBanner(files[0]);
        };
        image.src = data;
      };
      reader.readAsDataURL(fileData);
    },
    //上传banner图片
    async uploadBanner(file) {
      let self = this;
      let formdata = new FormData();
      formdata.append("file", file);
      let res = await fileRequest.getfileUploadFile(formdata);
      if (res.statusCode == 200) {
        self.$message({
          message: "上传图片成功",
          type: "success"
        });
        self.handImgVal = res.data;
        document.getElementById("fileUpImg").value = "";
      } else {
        self.$message(res.statusMessage);
        self.dataList = [];
      }
    },
    //保存
    async addClick() {
      let self = this;
      if (self.titleVal.length > 0) {
        let params = {
          content: self.briefVal,
          cover: self.handImgVal,
          pushTime: self.typeVal,
          title: self.titleVal
        };
        let res = await messageRequest.webMessageCreateAndSendFun(params);
        if (res.statusCode == 200) {
          self.$message("添加奖品成功！");
          this.$router.push({
            path: "/home/message"
          });
        } else {
          self.$message(res.statusMessage);
          self.dataList = [];
        }
      } else {
        self.$message("消息标题必填！");
      }
    }
  }
};
</script>
<style scoped>
.editHandImg {
  width: 200px;
  height: 100px;
}

.editUpImgHand {
  width: 200px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  background: #f2f5f7;
  color: #b2b7c0;
}

.editUpImgHand i {
  font-size: 20px;
}

.textBox {
  width: 350px;
  min-height: 80px;
  line-height: 24px;
  font-size: 14px;
  background: #f2f5f7;
  margin-left: 12px;
  padding: 4px 8px;
  margin-right: 14px;
  resize: none;
}

.issue {
  width: 450px;
}

.issueSelect {
  display: flex;
  margin: 12px;
}

.issueTrue {
  height: 40px;
  line-height: 40px;
}

.issueCheck {
  margin: 10px;
}

.isuueInd {
  width: 40px;
  height: 40px;
  line-height: 40px;
  text-align: center;
  color: #000;
}

.issueInp {
  width: 230px !important;
  height: 40px !important;
}

.issueButton {
  display: flex;
  margin: 12px;
}

.fontBlue {
  color: #2f3a5d;
  font-size: 14px;
  font-weight: bold;
}

.banckBlue {
  background: #279de2;
}

.banckRed {
  background: #ed4a47;
  margin: 0 24px;
}

.prizeDelect {
  background: #ed4a47;
  margin: 0 12px;
}

.prizeBox {
  width: 450px;
  padding: 4px 8px;
}

.prize {
  margin-bottom: 14px;
}

.prizeFoot {
  margin-top: 14px;
}

.prizeFoot,
.prizeTop {
  display: flex;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 14px;
  font-weight: bold;
  color: #2f3a5d;
}

.prizeTopInd {
  margin-right: 14px;
}

.prizeTopSelect {
  margin: 0 14px;
}

.prizeFootSelect {
  margin: 0 14px;
}

.prizeFootInp {
  width: 60px;
  height: 40px;
  margin: 0 14px;
  padding: 0 5px;
  background: #f2f5f7;
}

.addPrize {
  width: 110px;
  height: 40px;
  font-size: 16px;
  line-height: 40px;
  text-align: center;
  cursor: pointer;
  background: #279de2;
  margin: 14px;
}

.explain {
  font-size: 14px;
  padding: 14px 8px;
  line-height: 24px;
  color: #2f3a5d;
  font-weight: bold;
}

.timeBox {
  display: flex;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 14px;
  font-weight: bold;
  margin-top: 10px;
  color: #2f3a5d;
}

.footButton {
  display: flex;
  width: 700px;
  align-items: center;
  margin-top: 124px;
  margin-left: 230px;
}

.addBut {
  width: 150px;
  height: 50px;
  border-radius: 25px;
  text-align: center;
  line-height: 50px;
  cursor: pointer;
  margin: 0 12px;
  background: #279de2;
  color: white;
}

.cancelBut {
  width: 150px;
  height: 50px;
  border-radius: 25px;
  text-align: center;
  line-height: 50px;
  cursor: pointer;
  margin: 0 12px;
  border: 1px solid #279de2;
  color: #279de2;
}
</style>